package vsy.buzzwordbingo.model.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import vsy.buzzwordbingo.model.Game;
import vsy.buzzwordbingo.model.GameBoard;
import vsy.buzzwordbingo.model.Player;

public class PlayerImpl implements Player {
	private String name;
	private List<GameBoard> boards;
	private List<Game> games;
	private long loginTime;
	private String password;

	public PlayerImpl(String name, String password) {
		this.name = name;
		this.password = password;
		this.loginTime = System.currentTimeMillis();
		this.games = new ArrayList<Game>();
		this.boards = new ArrayList<GameBoard>();
	}

	@Override
	public String getName() {
		return this.name;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof Player))
			return false;
		
		Player p = (Player) obj;
		return p.getName().equals(this.name);
	}
	
	@Override
	public int hashCode() {
		return this.name.hashCode();
	}
	
	@Override
	public String toString() {
		return getName();
	}

	@Override
	public List<Game> getGames() {
		return Collections.unmodifiableList(this.games);
	}

	@Override
	public List<GameBoard> getBoards() {
		return Collections.unmodifiableList(this.boards);
	}

	@Override
	public void joinGame(Game game, GameBoard board) {
		if (!this.games.contains(game)) {
			this.games.add(game);
		}
		if (!this.boards.contains(board)) {
			this.boards.add(board);
		}
	}
	
	@Override
	public void leaveGame(Game game, GameBoard board) {
		if (this.games.contains(game)) {
			this.games.remove(game);
		}
		if (this.boards.contains(board)) {
			this.boards.remove(board);
		}
	}

	@Override
	public long getLoginTime() {
		return loginTime;
	}

	@Override
	public boolean matchesPassword(String password) {
		return this.password.equals(password);
	}

	@Override
	public void setPassword(String pass) {
		if (password == null) {
			password = pass;
		}
	}	
}
